package club.justwill.single.persistence.dao;

import club.justwill.single.persistence.jpo.UserActivity;
import club.justwill.single.persistence.support.BaseDao;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by xiangnan on 16-1-15.
 */
@Repository
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class UserActivityDao extends BaseDao<UserActivity> {

    public List getByUserId(Long userId) {
        return em.createNamedQuery("UserActivity.getByUserId").setParameter("userId", userId).getResultList();
    }

    public UserActivity getByUser8ActivityId(Long userId, Long activityId) {
        Query query = em.createNamedQuery("UserActivity.getByUser8ActivityId").setParameter("userId", userId).setParameter("activityId", activityId);
        try {
            return (UserActivity) query.getSingleResult();
        } catch (NonUniqueResultException e) {
            return (UserActivity) query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }

}
